package com.viewtouchchange;


import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

public class ListenerTouchViewFlipper implements View.OnTouchListener{
	
	
	private ViewFlipper vf;
	private float init_x;
	private float distance;
	private int viewCurrent = 1;
	private int minview = 1;
	private int maxview = 2;
	
	
	
	public ListenerTouchViewFlipper(ViewFlipper vf){
		this.vf=vf;
	}
	
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    	
   final View currentview = vf.getCurrentView();
   
   
       switch (event.getAction()) {

       case MotionEvent.ACTION_DOWN: //Cuando el usuario toca la pantalla por primera vez
            init_x=event.getX();
            
            return true;
            
       case MotionEvent.ACTION_MOVE:
    	   init_x=event.getX();
    	   distance = init_x-event.getX();
    	   currentview.layout((int)(event.getX()-distance), currentview.getTop(), currentview.getRight(), currentview.getBottom());
           if(distance>0 && viewCurrent<maxview)
            {
        	   vf.getChildAt(vf.getDisplayedChild() - 1); // next
        	   /*vf.setInAnimation(inFromRightAnimation());
                vf.setOutAnimation(outToLeftAnimation());
                vf.showPrevious();*/
                
                viewCurrent += 1;
            }

            if(distance<0 && viewCurrent>minview)
            {	
            	vf.getChildAt(vf.getDisplayedChild() + 1); // next
            	/*vf.setInAnimation(inFromLeftAnimation());
                vf.setOutAnimation(outToRightAnimation());
                vf.showNext(); */  
                viewCurrent -= 1;
            }
    	   
       break;
       
        case MotionEvent.ACTION_UP: //Cuando el usuario deja de presionar
           
            
            

        default:
            break;
        }
        return false;
    }
    
    private Animation inFromRightAnimation() {
    	 
        Animation inFromRight = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
        Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f );
        inFromRight.setDuration(500);
        inFromRight.setInterpolator(new AccelerateInterpolator());
 
        return inFromRight;
    }
    
    private Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoLeft.setDuration(500);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }
 
    private Animation inFromLeftAnimation() {
        Animation inFromLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromLeft.setDuration(500);
        inFromLeft.setInterpolator(new AccelerateInterpolator());
        return inFromLeft;
    }
 
    private Animation outToRightAnimation() {
        Animation outtoRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoRight.setDuration(500);
        outtoRight.setInterpolator(new AccelerateInterpolator());
        return outtoRight;
    }
    
}
